package com.sjc.springbootdemo.web;

import com.sjc.springbootdemo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

/**
 * @Classname TestController
 * @Description
 * @Date 2020/4/23 16:23
 * @Created by SangJiacun
 */
@Controller
@RequestMapping("/test")
public class TestController {
    /**
     * spring boot提供的RestTemplate
     */
    @Autowired
    RestTemplateBuilder restTemplateBuilder;

    // 如果说发送过来的请求是/hello/leo
    // 那么此时{name}这个东西就是leo，同时@PathVariable注解，会将{name}代表的值，注入方法入参
    // String name这个参数
    @RequestMapping("/hello/{name}")
    public String sayHello(@PathVariable("name") String name) {
        return "hello, " + name +", this is spring boot demo";
    }

    @GetMapping("/user/{id}")
    public User testGetUserById(@PathVariable("id") Long id) {
        RestTemplate client = restTemplateBuilder.build();
        String uri = "http://localhost:8080/api/v1.0/user/{id}";
        return client.getForObject(uri, User.class, id);
    }

}
